import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import {ConfigModule,ConfigService} from 'nestjs-config'
import * as path from 'path';

@Module({
  imports: [
    // 配置加载配置文件
    ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}'), {
      modifyConfigName: name => name.replace('.config', ''),
    }),
    TypeOrmModule.forRootAsync({
      useFactory:async (config: ConfigService)=>({
        type: config.get('database.type'),
        host: config.get('database.host'),
        port: config.get('database.port'),
        username: config.get('database.username'), 
        password: config.get('database.password'), 
        database: config.get('database.database'), 
        entities: [__dirname + '/**/*.entity{.ts,.js}'], 
        synchronize: true, // 自动同步数据模型到数据表中
        logging: config.get('database.logging'),
      }),
      inject:[ConfigService]
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
